package com.geo.smallcredit.vo;

import java.io.File;

import android.os.Environment;

public class FolderManager {
	
	private static final String APP_ROOT_FOLDER_OLD = "xiaoxinyong";
	private static final String APP_ROOT_FOLDER = "selector";
	private static final String APP_CAMERA_FOLDER = "finacial";
	private static final String APP_AVATAR_FOLDER = "Avatars";
	
	private static final File parentPath = Environment
			.getExternalStorageDirectory();
	
	private static String storagePath = "";
	private static String avatarsPath = "";
	private static String cameraTempPath = "";
	
	public static final String ILCACHE = APP_ROOT_FOLDER + "/" + APP_AVATAR_FOLDER;
		
	public static String getRootPath() {
		if (storagePath.equals("")) {
			storagePath = parentPath.getAbsolutePath() + "/" + APP_ROOT_FOLDER;			
			File path = new File(storagePath);
			if (!path.exists()) {
				path.mkdir();
			}
		}
		return storagePath;
	}
	
	// 将旧版本的Camera数据迁移到新的APP之下.
	public static void transferFile() {		
		String old = parentPath.getAbsolutePath() + 
				"/" + APP_ROOT_FOLDER_OLD + 
				"/" + APP_CAMERA_FOLDER;
		File oldF = new File(old);
		if (oldF.exists()) {
			String meigua = parentPath.getAbsolutePath() + 
					"/" + APP_ROOT_FOLDER + 
					"/" + APP_CAMERA_FOLDER;
			
			File path = new File(meigua);
			if (!path.exists()) {
				path.mkdir();
			}
			
			File files[] = oldF.listFiles();
			for (int i = 0; i < files.length; i++) {
				String name = files[i].getName();
				files[i].renameTo(new File(meigua + "/" + name));
			}
			oldF.delete();
			
			File oldAvatar = new File(parentPath.getAbsolutePath() +
					"/" + APP_ROOT_FOLDER_OLD + "/" + APP_AVATAR_FOLDER);
			if (oldAvatar.exists()) {
				File avatList[] = oldAvatar.listFiles();
				for (int i = 0; i < avatList.length; i++) {
					avatList[i].delete();
				}				
				oldAvatar.delete();
			}
			
			File oldRoot = new File(parentPath.getAbsolutePath() +
					"/" + APP_ROOT_FOLDER_OLD);
			if (oldRoot.exists()) {
				oldRoot.delete();
			}
		}
	}
	
	public static String getCameraTempPath() {
		if (cameraTempPath.equals("")) {
			cameraTempPath = getRootPath() + "/" + APP_CAMERA_FOLDER;
			File path = new File(cameraTempPath);
			if (!path.exists()) {
				path.mkdir();
			}
		}
		return cameraTempPath;
	}
	
	public static String getAartarPath() {
		if (avatarsPath.equals("")) {
			avatarsPath = getRootPath() + "/" + APP_AVATAR_FOLDER;
			File path = new File(avatarsPath);
			if (!path.exists()) {
				path.mkdir();
			}
		}
		return avatarsPath;
	}
}